<?php
/**
 * Created by PhpStorm.
 * User: wx:wenyi2048
 * Date: 2022/3/15 0015
 * Time: 16:47
 */

namespace app\admin\controller\mk;
/*商家管理，主要管理员*/
\think\Loader::import('controller/Controller', \think\Config::get('traits_path'), EXT);

use app\common\service\StoreService;
use app\common\service\StoreTypeService;
use app\admin\Controller;
use app\common\service\TypeService;
use app\common\service\WxpayService;
use think\Exception;


class Stores extends Controller
{
    use \app\admin\traits\controller\Controller;

    /**
     * @return string
     * @throws \think\Exception 重写controller index方法
     */
    public function index(){
        $param = $this->request->param();
        $service = new StoreService();//数据处理

        $typeService = new TypeService();
        $typeTree = $typeService->treeList();
        $this->view->assign('typeTree',$typeTree);

        $service->searchPage($this->view,$param);
        return $this->view->fetch();
    }

    public function select(){
        $this->index();
        return $this->view->fetch();
    }

    /**
     * @return string
     * @throws \think\Exception 重写controller recycleBin方法
     */
    public function recycleBin()
    {
        $param = $this->request->param();
        $service = new StoreService();
        $storeService = new StoreService();
        $storeTypeService = new StoreTypeService();

        $service->searchPage($this->view,$param,1);

        $this->view->assign('store',$storeService->getStore(@$param['admin_id']));
        $this->view->assign('storeTypes',$storeTypeService->getStoreType(@$param['admin_id']));

        return $this->view->fetch();
    }

    //商家管理模块
    public function business(){
        $sService = new StoreService();
        $storeId = $this->request->param('store_id');
        $store = $sService->getAdminStore($storeId);

        $this->view->assign('address',getAddress());
        $this->view->assign('store',$store);


        //快速处理
        $thumbs = @$store['thumb'];
        if($thumbs) $thumbs = explode(',',$thumbs);

        $address_name = @$store['province'].'/'.@$store['city'].'/'.@$store['area'];
        $this->view->assign('thumbs',$thumbs);
        $this->view->assign('address_name',$address_name);

        return $this->view->fetch();
    }

    //商家信息保存
    public function businessSave(){
        $sService = new StoreService();
        if($res = $sService->businessSave($this->request->param())){
            return ajax_return_adv($res);
        }
        return ajax_return_adv_error();
    }

    protected function beforeAdd(){
        $typeService = new TypeService();
        $typeTree = $typeService->treeList();
        $this->view->assign('typeTree',$typeTree);
        $this->view->assign('address',getAddress());
        $this->view->assign('thumbs',[]);
    }

    protected function afterEdit(){
        $this->beforeAdd();
        $vo = $this->view->__get('vo');
        $address_name = $vo['province'].'/'.$vo['city'].'/'.$vo['area'];
        $thumbs = @$vo['thumb'];
        if($thumbs) $thumbs = explode(',',$thumbs);
        $this->view->assign('thumbs',$thumbs);
        $this->view->assign('address_name',$address_name);
        $this->view->assign('address',getAddress());


        $tfid = $vo['types_first_id'];
        $tsid = $vo['types_second_id'];
        $ttid = $vo['types_third_id'];
        $types = (new TypeService())->getTypes([$tfid,$tsid,$ttid]);
        if($types) $vo['types_name'] = $types[0]['name'].'/'.$types[1]['name'].'/'.$types[2]['name'];
        else $vo['types_name'] = '暂未选择';
        $this->view->assign('vo', $vo);
    }

    protected function afterClEdit($ret){
        //$Wxrefund = new WxpayService();
        //$result=  $Wxrefund->addSpoils($ret['spoils_no'],$ret['name'],$ret['body_type']);
    }

    /**
     * @param $ret 添加店铺管理人员
     */
    protected function overAdd($ret){
        $id = $ret;
        if(@$ret['id']){
            $id = $ret['id'];
        }
        $param = $this->request->param();
        $param['id'] = $id;

        $storeService = new StoreService();
        $storeService->overAdd($param);

        //添加分账信息
        /*
        if(!@$ret['spoils_no']){
            $ret = Stores::find($id);
        }
        $Wxrefund = new WxpayService();
        $result=  $Wxrefund->addSpoils($ret['spoils_no'],$ret['name'],$ret['body_type']);
        if(!$result['data']=='SUCCESS'){
            return ajax_return_adv_error('添加分账信息失败');
        }*/
    }

}